package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC Java DataBase Connectivity
 * JDBC是java官方提供的一套接口，以标准的操作与任何数据库交互
 * 不同的数据库提供商会实现一套JDBC
 */
public class JDBCDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2与数据库连接
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("与数据库链接!");
        //3创建语句执行对象
        Statement statement = connection.createStatement();

        //4执行创建表操作
        String sql = "CREATE TABLE userinfo ( " +
                "    id          INT PRIMARY KEY AUTO_INCREMENT, " +
                "    username    VARCHAR(32),\n" +
                "    password    VARCHAR(32),\n" +
                "    nickname    VARCHAR(32),\n" +
                "    age         INT(3)\n" +
                ")";
        statement.execute(sql);
        System.out.println("表创建成功");
        connection.close();
    }
}
